package com.ruoyi.householdacidsys.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.buildingacidsys.domain.BuildingNucleicAcid;
import com.ruoyi.buildingacidsys.service.IBuildingNucleicAcidService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.householdacidsys.domain.HouseholdNucleicAcid;
import com.ruoyi.householdacidsys.service.IHouseholdNucleicAcidService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 住户核酸任务Controller
 * 
 * @author ruoyi
 * @date 2024-07-28
 */
@RestController
@RequestMapping("/householdacidsys/householdacid")
public class HouseholdNucleicAcidController extends BaseController
{
    @Autowired
    private IHouseholdNucleicAcidService householdNucleicAcidService;
    @Autowired
    private IBuildingNucleicAcidService buildingNucleicAcidService;
    /**
     * 查询住户核酸任务列表
     */
    @PreAuthorize("@ss.hasPermi('householdacidsys:householdacid:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseholdNucleicAcid householdNucleicAcid)
    {
        startPage();
        List<HouseholdNucleicAcid> list = householdNucleicAcidService.selectHouseholdNucleicAcidList(householdNucleicAcid);
        return getDataTable(list);
    }

    /**
     * 导出住户核酸任务列表
     */
    @PreAuthorize("@ss.hasPermi('householdacidsys:householdacid:export')")
    @Log(title = "住户核酸任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseholdNucleicAcid householdNucleicAcid)
    {
        List<HouseholdNucleicAcid> list = householdNucleicAcidService.selectHouseholdNucleicAcidList(householdNucleicAcid);
        ExcelUtil<HouseholdNucleicAcid> util = new ExcelUtil<HouseholdNucleicAcid>(HouseholdNucleicAcid.class);
        util.exportExcel(response, list, "住户核酸任务数据");
    }

    /**
     * 获取住户核酸任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('householdacidsys:householdacid:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(householdNucleicAcidService.selectHouseholdNucleicAcidById(id));
    }

    /**
     * 新增住户核酸任务
     */
    @PreAuthorize("@ss.hasPermi('householdacidsys:householdacid:add')")
    @Log(title = "住户核酸任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseholdNucleicAcid householdNucleicAcid)
    {

        List<BuildingNucleicAcid> buildingNucleicAcids = buildingNucleicAcidService.selectAllBuildingNucleicAcid();
        for (BuildingNucleicAcid buildingNucleicAcid:buildingNucleicAcids) {
            if(householdNucleicAcid.getCommunityName().equals(buildingNucleicAcid.getCommunityName())&&
            householdNucleicAcid.getBuildingName().equals(buildingNucleicAcid.getBuildingName()))
            {
            householdNucleicAcid.setBeginTime(buildingNucleicAcid.getBeginTime());
            householdNucleicAcid.setEndTime(buildingNucleicAcid.getEndTime());
            }
        }
        //状态默认为0 未开始检测
        householdNucleicAcid.setStatus("0");
        return toAjax(householdNucleicAcidService.insertHouseholdNucleicAcid(householdNucleicAcid));
    }

    /**
     * 修改住户核酸任务
     */
    @PreAuthorize("@ss.hasPermi('householdacidsys:householdacid:edit')")
    @Log(title = "住户核酸任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseholdNucleicAcid householdNucleicAcid)
    {
        return toAjax(householdNucleicAcidService.updateHouseholdNucleicAcid(householdNucleicAcid));
    }

    /**
     * 删除住户核酸任务
     */
    @PreAuthorize("@ss.hasPermi('householdacidsys:householdacid:remove')")
    @Log(title = "住户核酸任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(householdNucleicAcidService.deleteHouseholdNucleicAcidByIds(ids));
    }
}
